package com.example.smartcity_poverty.ui.AllService.YouthPost.activity

import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.smartcity_poverty.App.Companion.url
import com.example.smartcity_poverty.Base.BaseRecyclerAdapter
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.model.YhyoutPost.YhListAll
import com.example.smartcity_poverty.logic.network.AppApi
import com.example.smartcity_poverty.logic.network.ServiceNET.Companion.create
import com.example.smartcity_poverty.logic.util.Util.glide
import com.example.smartcity_poverty.logic.util.Util.show
import kotlinx.android.synthetic.main.activity_more_show.*
import kotlinx.android.synthetic.main.re_youthpost_more_list.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

/**
 * 人才区域展示
 */
class MoreShowActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_more_show)

        // 获取id
        intent.getStringExtra("id")?.toInt().let {
            // 填充数据
            setIdShow(it)
            // 填充数据列表
            setListAll(it!!)
        }


        tb.setNavigationOnClickListener {
            finish()
        }

    }

    /**
     * 填充数据
     */
    private fun setIdShow(id: Int?) {
        GlobalScope.launch(Dispatchers.Main){
            create<AppApi>().getYhIntroduceAll(
                getSharedPreferences("token", Context.MODE_PRIVATE).getString("token", "").toString(),
                id!!
            ).apply {
                if (this.code == 200){
                    // 填充数据
                    glide(url + this.data?.imgUrl.toString(), mShowImage)
                    mShowContent.text = "简介: ${this.data?.introduce}"
                }
            }
        }
    }

    /**
     * 人才政策列表
     */
    private fun setListAll(id: Int){
        val array = ArrayList<YhListAll.DataBean>()

        GlobalScope.launch(Dispatchers.IO){
            create<AppApi>().getYhListAll(
                getSharedPreferences("token", Context.MODE_PRIVATE).getString("token", "").toString(),
                id
            ).apply {
                if (this.code == 200){
                    array.clear()
                    array.addAll(this.data!!)

                    runOnUiThread {
                        // 填充数据
                        mShowListRv.apply {
                            this.layoutManager = LinearLayoutManager(context)
                            this.adapter = RvListAdapter(array, R.layout.re_youthpost_more_list)
                            this.isNestedScrollingEnabled = false
                        }
                    }

                }else runOnUiThread { this.msg?.show() }
            }
        }
    }


    /**
     * 列表适配器
     */
    class RvListAdapter(
        val list: MutableList<YhListAll.DataBean>,
        val layoutId: Int
    ) : BaseRecyclerAdapter<YhListAll.DataBean>(list, layoutId){
        override fun setData(
            view: BaseViewHolder.ViewFind,
            data: YhListAll.DataBean,
            position: Int,
            holder: BaseViewHolder
        ) {
            val d = list[position]
            val v = holder.viewFind

            // 填充数据
            v.listShowTitle.text = d.title
            v.listShowTime.text = "发布时间: ${d.createTime}"

            holder.itemView.setOnClickListener {
                v.view?.context?.startActivity(Intent(v.view?.context, ListShowActivity::class.java).putExtra("id", d.id.toString()))
            }
        }

    }


}